/*
    This source file is part of GeNa project.
    Copyright © GeNa project 2008
    For the latest info, see http://code.google.com/p/genaproject/ or  http://lumpyproject.forums-rpg.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>. */

#ifndef EDITOR_WINDOWAPP_H_INCLUDED
#define EDITOR_WINDOWAPP_H_INCLUDED

#ifndef WX_PCH_H_INCLUDED
#include "wx_pch.h"
#endif
#include <wx/clipbrd.h>

#include <boost/function.hpp>
#include <ostream>
#include <fstream>

#include "main_frame.h"
#include "singleton.h"
#include "windows_manager.h"
#include "wx_serialization_helper.h"

#define appInstance MAKE_SINGLETON_PSEUDO_INSTANCE(Application)

extern const wxEventType wxEVT_CALLFUNCTION;
//DECLARE_EVENT_TYPE(wxEVT_CALLFUNCTION, -1)

class Application : public wxApp
{
private:
    static Application* sInst;
    wxTimer mTimer;
    bool glewInitialized;

    friend class MainFrame;
public:
//    void OnAssertFailure(const wxChar *file, int line, const wxChar *func, const wxChar *cond, const wxChar *msg)
//    {
//        wxApp::OnAssertFailure(file, line, func, cond, msg);
//        wxTrap();
//    }

    bool initOgre;
#ifdef OGREJVP
    Ogre::Root *mRoot;
    Ogre::RenderSystem* m_rs;
    #endif // OGREJVP
    class MainFrame* m_frame;

    std::ofstream m_appOut;
    wxLog* m_appLog;

    bool m_useDefaultWindowsManager;
    Application();

    virtual bool OnInit();

    static Application* getInstance()
    {
        return sInst;
    }

private:
    friend class GLWindow;


    void setupScene();

    unsigned int autosaveTime;
    unsigned int autosaveInterval;
private:
    enum
    {
        ID_RENDERTIMER,
        ID_CALLFUNCTION
    };

    struct CallFunctionStruct
    {
        wxMutex* mutex;
        wxCondition* condition;

        boost::function0<void> f;
    };

    void onRenderTimer(wxTimerEvent &event);

    void onCallFunction(wxCommandEvent &event)
    {
        CallFunctionStruct* s = (CallFunctionStruct*)event.GetClientData();
        (s->f)();

        wxMutexLocker(*s->mutex);
        s->condition->Broadcast();
    }

public:
    void callFunction(boost::function0<void> f)
    {
        CallFunctionStruct s;
        s.f = f;

        wxMutex* mutex = new wxMutex;
        wxCondition* condition = new wxCondition(*mutex);
        mutex->Lock();

        s.mutex = mutex;
        s.condition = condition;

        wxCommandEvent evt(wxEVT_CALLFUNCTION,0);
        evt.SetClientData((void*)&s);

        if( wxIsMainThread())
            onCallFunction(evt);
        else
        {
            AddPendingEvent(evt);
            condition->Wait();
        }
    }

private:
    DECLARE_EVENT_TABLE()
};


#endif // EDITOR_WINDOWAPP_H_INCLUDED
